package com.ruoyi.cmenonmetal.service.impl;

import com.ruoyi.cmenonmetal.domain.*;
import com.ruoyi.cmenonmetal.mapper.*;
import com.ruoyi.cmenonmetal.service.IZxNonmetalOrderFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * NonmetalOrderService业务层处理
 * 
 * @author zhangxu
 * @date 2021-12-16
 */
@Service
public class ZxNonmetalOrderFormServiceImpl implements IZxNonmetalOrderFormService 
{
    @Autowired
    private ZxNonmetalOrderFormMapper zxNonmetalOrderFormMapper;
    @Autowired
    private ZsDataPreprocessMapper zsDataPreprocessMapper;
    @Autowired
    private ZsPrintProcessMapper zsPrintProcessMapper;
    @Autowired
    private ZsPostProcessMapper zsPostProcessMapper;
    @Autowired
    private ZsOrderUrlMapper zsOrderUrlMapper;


    /**
     * 查询NonmetalOrder
     * 
     * @param id NonmetalOrder主键
     * @return NonmetalOrder
     */
    @Override
    public ZxNonmetalOrderForm selectZxNonmetalOrderFormById(Long id)
    {
        return zxNonmetalOrderFormMapper.selectZxNonmetalOrderFormById(id);
    }

    /**
     * 查询NonmetalOrder列表
     * 
     * @param zxNonmetalOrderForm NonmetalOrder
     * @return NonmetalOrder
     */
    @Override
    public List<ZxNonmetalOrderForm> selectZxNonmetalOrderFormList(ZxNonmetalOrderForm zxNonmetalOrderForm)
    {
        return zxNonmetalOrderFormMapper.selectZxNonmetalOrderFormList(zxNonmetalOrderForm);
    }

    /**
     * 新增NonmetalOrder
     * 
     * @param zxNonmetalOrderForm NonmetalOrder
     * @return 结果
     */
    @Override
    @Transactional
    public int insertZxNonmetalOrderForm(ZxNonmetalOrderForm zxNonmetalOrderForm)
    {
        //新增预处理数据
        ZsDataPreprocess zsDataPreprocess = new ZsDataPreprocess();
        zsDataPreprocess.setOrderId(zxNonmetalOrderForm.getOrderId());
        zsDataPreprocessMapper.insertZsDataPreprocess(zsDataPreprocess);

        //新增打印处理数据
        ZsPrintProcess zsPrintProcess = new ZsPrintProcess();
        zsPrintProcess.setOrderId(zxNonmetalOrderForm.getOrderId());
        zsPrintProcessMapper.insertZsPrintProcess(zsPrintProcess);

        //新增后处理数据
        ZsOrderUrl zsOrderUrl = new ZsOrderUrl();
        zsOrderUrl.setOederid(zxNonmetalOrderForm.getOrderId());
        zsOrderUrlMapper.insertZsOrderUrl(zsOrderUrl);

        //新增后处理数据
        ZsPostProcess zsPostProcess = new ZsPostProcess();
        zsPostProcess.setOrderId(zxNonmetalOrderForm.getOrderId());
        zsPostProcessMapper.insertZsPostProcess(zsPostProcess);

        return zxNonmetalOrderFormMapper.insertZxNonmetalOrderForm(zxNonmetalOrderForm);
    }

    /**
     * 修改NonmetalOrder
     * 
     * @param zxNonmetalOrderForm NonmetalOrder
     * @return 结果
     */
    @Override
    public int updateZxNonmetalOrderForm(ZxNonmetalOrderForm zxNonmetalOrderForm)
    {
        return zxNonmetalOrderFormMapper.updateZxNonmetalOrderForm(zxNonmetalOrderForm);
    }

    /**
     * 批量删除NonmetalOrder
     * 
     * @param ids 需要删除的NonmetalOrder主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteZxNonmetalOrderFormByIds(Long[] ids)
    {


        ZxNonmetalOrderForm zxNonmetalOrderForm = null;

        ZsDataPreprocess zsDataPreprocess = null;
        List<ZsDataPreprocess> zsDataPreprocesses = null;



        ZsPrintProcess zsPrintProcess = null;
        List<ZsPrintProcess> zsPrintProcesses = null;

        ZsOrderUrl zsOrderUrl = null;
        List<ZsOrderUrl> zsOrderUrls = null;

        ZsPostProcess zsPostProcess = null;
        List<ZsPostProcess> zsPostProcesses = null;

        int i;

        if (ids.length > 0) {
            for (Long id : ids) {
                i = 0;
                 zxNonmetalOrderForm = zxNonmetalOrderFormMapper.selectZxNonmetalOrderFormById(id);

                //删除预处理相关数据
                zsDataPreprocess = new ZsDataPreprocess();
                zsDataPreprocess.setOrderId(zxNonmetalOrderForm.getOrderId());
                zsDataPreprocesses = zsDataPreprocessMapper.selectZsDataPreprocessList(zsDataPreprocess);
                zsDataPreprocessMapper.deleteZsDataPreprocessById(zsDataPreprocesses.get(0).getId());



                //删除打印处理数据
                zsPrintProcess = new ZsPrintProcess();
                zsPrintProcess.setOrderId(zxNonmetalOrderForm.getOrderId());
                zsPrintProcesses = zsPrintProcessMapper.selectZsPrintProcessList(zsPrintProcess);
                zsPrintProcessMapper.deleteZsPrintProcessById(zsPrintProcesses.get(0).getId());

                //删除orderurl相关数据
                zsOrderUrl = new ZsOrderUrl();
                zsOrderUrl.setOederid(zxNonmetalOrderForm.getOrderId());
                zsOrderUrls= zsOrderUrlMapper.selectZsOrderUrlList(zsOrderUrl);
                zsOrderUrlMapper.deleteZsOrderUrlById(zsOrderUrls.get(0).getId());

                //删除后处理数据
                zsPostProcess = new ZsPostProcess();
                zsPostProcess.setOrderId(zxNonmetalOrderForm.getOrderId());
                zsPostProcesses = zsPostProcessMapper.selectZsPostProcessList(zsPostProcess);
                zsPostProcessMapper.deleteZsPostProcessById(zsPostProcesses.get(0).getId());

                i++;

            }
        }

        return zxNonmetalOrderFormMapper.deleteZxNonmetalOrderFormByIds(ids);
    }

    /**
     * 删除NonmetalOrder信息
     * 
     * @param id NonmetalOrder主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteZxNonmetalOrderFormById(Long id)
    {
        ZxNonmetalOrderForm zxNonmetalOrderForm = zxNonmetalOrderFormMapper.selectZxNonmetalOrderFormById(id);

        //删除相关预处理数据
        ZsDataPreprocess zsDataPreprocess =new ZsDataPreprocess();
        zsDataPreprocess.setOrderId(zxNonmetalOrderForm.getOrderId());
        List<ZsDataPreprocess> zsDataPreprocesses = zsDataPreprocessMapper.selectZsDataPreprocessList(zsDataPreprocess);
        zsDataPreprocessMapper.deleteZsDataPreprocessById(zsDataPreprocesses.get(0).getId());

        //删除相关打印处理相关数据
        ZsPrintProcess zsPrintProcess = new ZsPrintProcess();
        zsPrintProcess.setOrderId(zxNonmetalOrderForm.getOrderId());
        List<ZsPrintProcess> zsPrintProcesses = zsPrintProcessMapper.selectZsPrintProcessList(zsPrintProcess);
        zsPrintProcessMapper.deleteZsPrintProcessById(zsPrintProcesses.get(0).getId());

        //删除相关后处理相关数据
        ZsPostProcess zsPostProcess = new ZsPostProcess();
        zsPostProcess.setOrderId(zxNonmetalOrderForm.getOrderId());
        List<ZsPostProcess> zsPostProcesses = zsPostProcessMapper.selectZsPostProcessList(zsPostProcess);
        zsPostProcessMapper.deleteZsPostProcessById(zsPostProcesses.get(0).getId());

        //删除orderURL相关数据，
        ZsOrderUrl zsOrderUrl = new ZsOrderUrl();
        zsOrderUrl.setOederid(zxNonmetalOrderForm.getOrderId());
        List<ZsOrderUrl> zsOrderUrls = zsOrderUrlMapper.selectZsOrderUrlList(zsOrderUrl);
        zsOrderUrlMapper.deleteZsOrderUrlById(zsOrderUrls.get(0).getId());


        return zxNonmetalOrderFormMapper.deleteZxNonmetalOrderFormById(id);
    }
    /**
     * 根据时间查询当日NonmetalOrder
     *
     * @param begainDate 查询日期
     * @return 结果
     */
    @Override
    public List<ZxNonmetalOrderForm> selectNonmetalOrderBydate(Date begainDate) {
        return zxNonmetalOrderFormMapper.selectZxNonmetalOrderBydate(begainDate);
    }
}
